/*     */ package com.zimbra.cs.index.query;
/*     */ 
/*     */ import com.google.common.base.CharMatcher;
/*     */ import com.google.common.base.Joiner;
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.index.LuceneQueryOperation;
/*     */ import com.zimbra.cs.index.LuceneQueryOperation.LazyMultiPhraseQuery;
/*     */ import com.zimbra.cs.index.NoTermQueryOperation;
/*     */ import com.zimbra.cs.index.QueryOperation;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import java.io.IOException;
/*     */ import java.io.StringReader;
/*     */ import java.util.List;
/*     */ import org.apache.lucene.analysis.Analyzer;
/*     */ import org.apache.lucene.analysis.TokenStream;
/*     */ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
/*     */ import org.apache.lucene.index.Term;
/*     */ import org.apache.lucene.search.PhraseQuery;
/*     */ import org.apache.lucene.search.TermQuery;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TextQuery
/*     */   extends Query
/*     */ {
/*  49 */   private final List<String> tokens = Lists.newArrayList();
/*     */   private final String field;
/*     */   private final String text;
/*  52 */   private boolean quick = false;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public TextQuery(Analyzer analyzer, String field, String text)
/*     */   {
/*  59 */     this(analyzer.tokenStream(field, new StringReader(text)), field, text);
/*     */   }
/*     */   
/*     */   TextQuery(TokenStream stream, String field, String text) {
/*  63 */     this.field = field;
/*  64 */     this.text = text;
/*     */     try
/*     */     {
/*  67 */       CharTermAttribute termAttr = (CharTermAttribute)stream.addAttribute(CharTermAttribute.class);
/*  68 */       stream.reset();
/*  69 */       while (stream.incrementToken()) {
/*  70 */         this.tokens.add(termAttr.toString());
/*     */       }
/*  72 */       stream.end();
/*  73 */       stream.close();
/*     */     } catch (IOException e) {
/*  75 */       ZimbraLog.search.error("Failed to tokenize text=%s", new Object[] { text });
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void setQuick(boolean value)
/*     */   {
/*  89 */     this.quick = value;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String getField()
/*     */   {
/*  99 */     return this.field;
/*     */   }
/*     */   
/*     */   public boolean hasTextOperation()
/*     */   {
/* 104 */     return true;
/*     */   }
/*     */   
/*     */   public QueryOperation compile(Mailbox mbox, boolean bool) throws ServiceException
/*     */   {
/* 109 */     if ((this.quick) || (this.text.endsWith("*")))
/*     */     {
/* 111 */       String last = this.tokens.isEmpty() ? this.text : (String)this.tokens.remove(this.tokens.size() - 1);
/* 112 */       LuceneQueryOperation.LazyMultiPhraseQuery query = new LuceneQueryOperation.LazyMultiPhraseQuery();
/* 113 */       for (String token : this.tokens) {
/* 114 */         query.add(new Term(this.field, token));
/*     */       }
/* 116 */       query.expand(new Term(this.field, CharMatcher.is('*').trimTrailingFrom(last)));
/*     */       
/* 118 */       LuceneQueryOperation op = new LuceneQueryOperation();
/* 119 */       op.addClause(toQueryString(this.field, this.text), query, evalBool(bool));
/* 120 */       return op; }
/* 121 */     if (this.tokens.isEmpty())
/*     */     {
/*     */ 
/* 124 */       return new NoTermQueryOperation(); }
/* 125 */     if (this.tokens.size() == 1) {
/* 126 */       LuceneQueryOperation op = new LuceneQueryOperation();
/* 127 */       op.addClause(toQueryString(this.field, this.text), new TermQuery(new Term(this.field, (String)this.tokens.get(0))), evalBool(bool));
/* 128 */       return op;
/*     */     }
/* 130 */     assert (this.tokens.size() > 1) : this.tokens.size();
/* 131 */     PhraseQuery query = new PhraseQuery();
/* 132 */     for (String token : this.tokens) {
/* 133 */       query.add(new Term(this.field, token));
/*     */     }
/* 135 */     LuceneQueryOperation op = new LuceneQueryOperation();
/* 136 */     op.addClause(toQueryString(this.field, this.text), query, evalBool(bool));
/* 137 */     return op;
/*     */   }
/*     */   
/*     */ 
/*     */   public void dump(StringBuilder out)
/*     */   {
/* 143 */     out.append(this.field);
/* 144 */     out.append(':');
/* 145 */     Joiner.on(',').appendTo(out, this.tokens);
/* 146 */     if ((this.quick) || (this.text.endsWith("*"))) {
/* 147 */       out.append("[*]");
/*     */     }
/*     */   }
/*     */   
/*     */   public void sanitizedDump(StringBuilder out)
/*     */   {
/* 153 */     out.append(this.field);
/* 154 */     out.append(':');
/* 155 */     out.append(Strings.repeat("$TEXT,", this.tokens.size()));
/* 156 */     if (out.charAt(out.length() - 1) == ',') {
/* 157 */       out.deleteCharAt(out.length() - 1);
/*     */     }
/* 159 */     if ((this.quick) || (this.text.endsWith("*"))) {
/* 160 */       out.append("[*]");
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/query/TextQuery.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */